<?php
	namespace Admin\Controller;
	use Think\Controller;
	use Think\Image;
	abstract class InController extends Controller{


		//查看历史
		abstract  function history($myArt);

		//注册用户个人首页展示(展示首页所有信息)
		public function Index(){
			$this->display();
		}
		//查询用户基本信息
		abstract public function userInfor();
		//查询伙伴动态
		abstract public function partner();
		//查询我的游记
		abstract public function myTravel();
		//插入数据,我的行程计划表
		abstract public function planAdd();
		//删除我的行程计划表
		abstract public function planDel();
		//更改我的行程计划表
		abstract public function planUp();
		//查询我的行程计划
		abstract public function plan();
		//插入游记文章
		abstract public function travelAdd();
		//查看自己的游记具体某篇文章
		abstract public function read();	
		//发布求助信息
		abstract public function helpAdd();
		//清空session
		abstract  function clear();
	}

	class IndexController extends InController{

		/**
		 * @author K.
		 * 添加构造函数
		 */
		/*public function __construct()
		{
			parent::__construct();
			//未登录则跳转至登录页面
			if (empty(cookie('username'))) {
				return redirect(U('/admin/user/login'));
			}
		}*/

		//查看历史
	public function history($myArt){
		//print_r($myArt);
		//exit();
		$row = session('?history')?session('history'):array();
		$g = array();
		$g['aid'] = I('get.id');
		$g['title'] = $myArt['title'];
		$g['cat_id'] = $myArt['cat_id'];
		$g['content'] = $myArt['content'];
		//print_r($g);
		//exit();
		//$g['img_path'] = $myArt['img_path'];
		$row[I('get.id')] = $g;

		if(count($row)>5){
			$key = key($row);
			unset($key);
		}
		session('history',$row);
	}

		//注册用户个人首页展示(展示首页所有信息)
		public function Index(){	
			$userInfors = $this->userInfor();		
			$this->assign('userInfros',$userInfors);	//模板中展示用户基本信息	
			$this->assign('history',array_reverse(session('history')));	
			$this->display();
		}

		//查询用户基本信息
		public function userInfor(){
			if(!cookie('username')){
				$this->redirect('/');
			}else{
				$value = cookie('username');
				$user = D('users')->where("username="."'".$value."'")->find();
				return $user;
			}	
		}
		//查询伙伴动态
		public function partner(){   //check默认为0,查询所有伙伴动态;为1查询游记动态;为2查询活动动态;为3查询求助动态
			//$check =a
			$user = $this->userInfor();
			$map['cat_id'] = array('in',[1,2,3]);
			$map['uid']	=	array('eq',$user['uid']);
			$map['power'] = array('in',[4,5]);
			$count = D('trends')->where($map)->count();
			$pager = new \Org\Util\BootStrapPage($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
		 	$page = $pager->show();
			//进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list =D('trends')->where($map)->order('pubtime desc')->limit($pager->firstRow.','.$pager->listRows)->select();
			//var_dump($list);
			//exit();
			$this->assign('page',$page);
			$this->assign('partners',$list);
			$this->display('section1'); 
		}

		//插入游记文章
		public function travelAdd(){
			$upload = new \Think\Upload();// 实例化上传类
			//$upload->maxSize = 3145728;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath = APP_PATH; // 设置附件上传根目录
			$img_savepath = $upload->savePath = '/Public/Admin/images/'; // 设置附件上传根目录
			//yuming/Admin/images/
			$info = $upload->upload();
			$thum_pic = $info['pic']['savepath'].$info['pic']['savename'];
			//var_dump(I());
			//exit();
			$arts = D('trends');
			$user = $this->userInfor();
			$arts->uid = $user['uid'];
			$arts->cat_id = I('cat_id');
			$arts->title = I('title');
			$arts->smtitle = I('smtitle');
			$arts->cutcon = I('content');
			$arts->content = I('content');
			$arts->power = I('power');
			$arts->img_path = $thum_pic;
			$arts->pubtime = time();
			if($arts->add()){
				$this->redirect('Index/index','',3,'发布成功,3秒后跳转!');
			}else{
				$this->redirect('Index/partner','',3,'发布失败,3秒后失败!');
			}
		}

		//查询我的游记列表
		 public function myTravel(){
		 	$user = $this->userInfor();
		 	$map['uid'] = array('eq',$user['uid']);
		 	$map['cat_id'] = array('eq',1);
		 	$count = D('trends')->where($map)->field('id,img_path,com_num,click_num,title,content,pubtime')->count();
		 	$pager = new \Org\Util\BootStrapPage($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
		 	$page = $pager->show();
			//进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list =D('trends')->where($map)->order('pubtime desc')->limit($pager->firstRow.','.$pager->listRows)->select();
			//var_dump($page);
			//exit();
			$this->assign('myTravles',$list);		//模板中展示用户游记
			$this->assign('page',$page);
			$this->display('section2');
		 }

		 //插入我的行程计划表
		public function planAdd(){
			$plan = D('plans');
			$user = $this->userInfor();
			$pl = I();
			$plan->area = $pl['area'];
			$plan->uid = $user['uid'];
			$plan->spot = $pl['spot'];
			$plan->routime = strtotime($pl['routime']);
			$plan->people = $pl['people'];
			$plan->pubtime = time();
			if($plan->add()){
				$this->redirect('Index/plan');
			}else{
				echo "失败!";
			}
		}
		//删除我的行程计划表
		public function planDel(){
			$plan = D('plans');
			if($plan->delete(I('id'))){
				$this->redirect('Index/plan');
			}else{
				echo "删除失败!";
			}
		}
		//更改我的行程计划表
		public function planUp(){		
			if(!IS_POST){											//判断是否有post提交
				$plan = D('plans');									//实例化Model
				$plans = $plan->find(I('id'));						//根据id查找数据库中指定要修改的数据
				$plans['routime'] = date('Y-m-d',$plans['routime']);//格式化数据库中的时间戳
				echo json_encode($plans);							//数据中数据格式化成json格式的数据
			}else{
				$row = I();											//获取的post及get数据存入$row变量中
				$row['routime'] = strtotime(I('post.routime'));
				//print_r($row);
				//exit();
				$plans = D('plans')->where("pid=".I('get.pid'))->save($row);//根据pid指定修改数据库中的数据
				if($plans){													//判断是否插入成功
					$this->redirect('Index/plan','',3,"修改成功,页面跳转中...");//插入成功,提示跳转
				}else{															
					$this->redirect('Index/plan','',3,'修改失败,页面跳转中...');//插入失败,提示跳转
				}
			}	
		}
		//查询我的行程计划
		public function plan(){
			$user = $this->userInfor();
			$count = D('plans')->count();
			$pager = new \Org\Util\BootStrapPage($count,3);	// 实例化分页类传入总记录数和每页显示的记录数
			$page = $pager->show();							// 分页显示输出
			//进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list =D('plans')->where("uid=$user[uid]")->order('routime desc')->limit($pager->firstRow.','.$pager->listRows)->select();
			$this->assign('page',$page);
			$this->assign('plans',$list);
			$this->display('section3');
			//print_r($list);
			//exit();
		}

		

		//查看自己的游记具体某篇文章
		public function read(){
			$aid = I('id');
			//echo $aid;
			$art = D('trends');
			$arts = $art->find($aid);
			$art->where('id='."'".$aid."'")->setInc('click_num'); // 
			$user = D('users')->find($arts['uid']);
			$this->history($arts);
			//$arts['author'] =$user['username'];
			//print_r(session('history'));
			//exit();
			$this->assign('history',array_reverse(session('history')));
			$this->assign('myArt',$arts);
			$this->display('art');
		}

		//发布求助信息
		public function helpAdd(){
			$arts = D('trends');
			$user = $this->userInfor();
			$arts->uid = $user['uid'];
			//echo $user['uid'];
			//print_r(I());
			//exit();
			$arts->power = I('power');
			$arts->cat_id = I('cat_id');
			$arts->title = I('title');
			$arts->area = I('area');
			$arts->phone = I('phone');
			$arts->content = I('content');
			$arts->pubtime = time();
			if($arts->add()){
				$this->redirect('Index/partner','',3,'发布成功,3秒后返回');
			}else{
				$this->redirect('Index/partner','',3,'发布失败,3秒后返回');
			}
		}

		/**
		 * @author K.
		 * 反馈获得的好友列表
		 */
		public function getFriends()
		{
			$user = M('users')->where('username=\''.cookie('username').'\'')->find();
			$friends  = M('friends')->field('users.uid, users.username')->join('users ON users.uid=friends.uid2')->where('uid1='.$user['uid'])->select();
		    echo json_encode($friends);
		}

		/**
		 * @author K.
		 * 接受处理活动提交表单
		 */
		public function postActivity() {
			//print_r($_POST);
			//exit();
			$user                    = M('users')->where('username=\''.cookie('username').'\'')->find();
			$data                        = [];
			$data['cat_id']			= array_pop($_POST);
			$data['power']			= array_pop($_POST);
			//print_r(I());
			//exit();
			$data['title']           = array_shift($_POST);
			$data['smtitle']        = array_shift($_POST);
			$data['area']			= array_shift($_POST);
			$data['content']         = array_shift($_POST);
			$num                     = count($_POST);
			$data['uid']            = $user['uid'];
			$data['pubtime']		= time();
			$data['inv_partner']          = implode(',', $_POST);
			$data['ace_partner'] = rtrim(str_repeat('0,', $num),',');
		    M('trends')->add($data);
		    $this->redirect('Index/partner','',3,'发布成功,3秒后返回');
		}


		public function clear(){
			session('history',null);
		}
	}
?>